<template>
  <!-- 最外层的容器 -->
  <view class="my-checkout-container">
    <!-- 全选区域 -->
    <label class="radio">
      <radio
        color="#C00000"
        :checked="isCheckedAll"
        @click="handleRadioClick(!isCheckedAll)"
      />
      <text>全选</text>
    </label>

    <!-- 合计区域 -->
    <view class="amount-box">
      合计:<text class="amount">￥{{ checkedAmount }}</text>
    </view>

    <!-- 结算按钮 -->
    <view class="btn-checkout" @click="handlePay"
      >结算({{ checkedTotal }})</view
    >
  </view>
</template>

<script>
import { mapGetters, mapState } from 'vuex'
export default {
  data() {
    return {}
  },
  computed: {
    ...mapGetters('cart', ['checkedTotal', 'total', 'checkedAmount']),
    ...mapState('user', ['token', 'address']),
    ...mapGetters('user', ['fullAddress']),
    // 计算属性控制如果购物车tab角标和结算数量一样，则必然是全选的
    isCheckedAll() {
      return this.checkedTotal === this.total && this.checkedTotal > 0
    },
  },
  methods: {
    // 点击全选和反选
    handleRadioClick(flag) {
      this.$store.commit('cart/updateAllState', flag)
    },

    // 支付
    handlePay() {
      if (this.checkedTotal === 0) return this.$msg('结算前请先勾选结算的商品')
      // if (JSON.stringify(this.address) === '{}') return this.$msg('结算前请先选择地址')
      if (!this.fullAddress) return this.$msg('结算前请先选择地址')
      if (!this.token) return this.$msg('结算前请先登录')
      console.log('支付了')
    },
  },
}
</script>

<style lang="scss">
.my-checkout-container {
  position: fixed;
  bottom: 0;
  left: 0;
  z-index: 999;
  width: 100%;
  height: 100rpx;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 10rpx;
  font-size: 28rpx;

  .radio {
    display: flex;
    align-items: center;
  }

  .amount {
    color: #c00000;
  }

  .btn-checkout {
    height: 100rpx;
    min-width: 200rpx;
    background-color: #c00000;
    color: white;
    line-height: 100rpx;
    text-align: center;
    padding: 0 20rpx;
  }
}
</style>
